/**
 * @desc: 解析路由
 * @param { Array } routerMap 后台返回的路由表
 * @return 返回
 */
const modules = import.meta.glob("../views/**/*.vue");
export const generateRouter = (routerMap: any) => {
  return routerMap.map((item: any) => {
    const currentRouter: any = {
      path: item.path,
      name: item.name,
      meta: item.meta,
      redirect: item.redirect
    };
    if (item.component && typeof item.component == "string") {
      currentRouter.component = modules[`../views${item.component}.vue`];
    }
    // 是否存在子路由
    item?.children?.length && (currentRouter.children = generateRouter(item.children));
    return currentRouter;
  });
};

/**
 * @description 使用递归过滤出需要渲染在左侧菜单的列表 (需剔除 isHide == true 的菜单)
 * @param {Array} menuList 菜单列表
 * @returns {Array}
 * */
export function getShowMenuList(menuList: Menu.MenuOptions[]) {
  let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList));
  return newMenuList.filter(item => {
    item.children?.length && (item.children = getShowMenuList(item.children));
    return !item.meta?.isHide;
  });
}
